package com.testdocs.client.version.list.add;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.testdocs.client.common.i18n.CommonConstants;
import com.testdocs.client.validation.ConstraintViolationException;
import com.testdocs.client.validation.FieldConstraint;
import com.testdocs.client.validation.ValidationConstraint;
import com.testdocs.client.version.VersionInfo;
import com.testdocs.client.version.i18n.VersionConstants;

public class NewVersionDialog {
	private static NewVersionDialogUiBinder uiBinder = GWT
			.create(NewVersionDialogUiBinder.class);

	@UiTemplate("NewVersionDialog.ui.xml")
	interface NewVersionDialogUiBinder extends UiBinder<Widget, NewVersionDialog> {
	}

	ActionConfirmedHandler handler;

	@UiField
	DialogBox dialogBox;
	@UiField
	Label nameLabel;
	@UiField
	Label basedOnLabel;
	@UiField
	ListBox basedOnList;
	@UiField
	TextBox nameTextBox;

	@UiField
	Label nameErrorLabel;
	
	@UiField
	Button okButton;
	@UiField
	Button cancelButton;
	
	private VersionConstants versionConstants = (VersionConstants) GWT.create(VersionConstants.class);
	private CommonConstants commonConstants = (CommonConstants) GWT.create(CommonConstants.class);

	public NewVersionDialog() {
		uiBinder.createAndBindUi(this);
		dialogBox.setText(versionConstants.create());
		nameLabel.setText(versionConstants.versionName());
		basedOnLabel.setText(versionConstants.basedOn());
		okButton.setText(commonConstants.ok());
		cancelButton.setText(commonConstants.cancel());
		
		basedOnList.setVisibleItemCount(15);
		basedOnList.setSize("15em", "15em");
	}

	@UiHandler("cancelButton")
	void handleCancel(ClickEvent e) {
		hide();
	}

	@UiHandler("okButton")
	void handleOk(ClickEvent e) {
		clearErrors();
		try {
			handler.handle(nameTextBox.getText(), basedOnList.getValue(basedOnList.getSelectedIndex()));
			hide();
		} catch (ConstraintViolationException exception) {
			for (ValidationConstraint constraint : exception.getValidationConstraints()) {
				if (constraint instanceof FieldConstraint &&
						((FieldConstraint)constraint).getField().equals("versionName")) {
					nameErrorLabel.setText(constraint.getMessage());
				}
			}
		}
	}

	public void show(List<VersionInfo> basedOn, ActionConfirmedHandler handler) {
		this.handler = handler;
		dialogBox.center();
		dialogBox.show();
		nameTextBox.setFocus(true);
		nameTextBox.setText("");
		clearErrors();
		basedOnList.clear();
		for(VersionInfo v : basedOn) {
			if (v.getId() != null) {
				basedOnList.addItem(v.getName(), v.getId());
			}
		}
		basedOnList.setItemSelected(0, true);
	}

	public void hide() {
		dialogBox.hide();
	}
	
	private void clearErrors() {
		nameErrorLabel.setText("");
	}

	public interface ActionConfirmedHandler extends EventHandler {
		public void handle(String name, String basedOnVersionId) throws ConstraintViolationException;
	}
}
